var http = require( '../../utils/util' )
import { restInformationBanner, restDeptGet } from '../../api/index.js';
import { restWechat, restUserCenter, restUserCenterByUserId } from '../../api/user.js';
var app = getApp()

Page({
	data: {
		imgUrls: [{images: '../../images/banner-1.jpg'},{images: '../../images/banner-1.jpg'}],
		indicatorDots: false,
		autoplay: false,
		interval: 5000,
		duration: 1000,
		
		mendianList: [],
		mendianPage: 1,
		mendianPageSize: 12,
		mendianFlag: true,
		recommendCode: 0,
		
		showauthflag: false
	},
	onLoad: function( options ) {
		var _that = this;
		this.setData(options);
		app.globalData.userInfo = http.getUserInfo();
		
		if(!app.globalData.userInfo || app.globalData.userInfo.length<=0){
			wx.hideTabBar();
			_that.setData({showauthflag: true});
		}
		
		
		wx.login({
		  success: res => {
			restWechat({js_code: res.code, recommendCode: _that.data.scene}).then(res=>{
				console.log( res )
				if(res.code == 0){
					app.globalData.wxlogin = res.data;
					
					_that.restInformationBanner();
					if(! app.globalData.userLOC){
						wx.getLocation({
							type: 'wgs84',
							success (res) {
								console.log(res);
							   app.globalData.userLOC = res;
							   _that.getDept();
							}
						});
					}else{
						_that.getDept();
					}
				}
			});
		  }
		});
	},
	onReady: function () {
		// var _that = this;
		// _that.restInformationBanner();
		
		// if(! app.globalData.userLOC){
		// 	wx.getLocation({
		// 		type: 'wgs84',
		// 		success (res) {
		// 			console.log(res);
		// 		   app.globalData.userLOC = res;
		// 		   _that.getDept();
		// 		}
		// 	});
		// }else{
		// 	_that.getDept();
		// }
		
	},
	onShow: function () {
		//每次查看门店是否有数据, 没有数据,判断用户位置
		
	},
	
	getDept(){
		var _that = this;
		
		if(! _that.data.mendianFlag) return;
		
		console.log(app.globalData.wxlogin);
		var userId = '';
		if(app.globalData.wxlogin) userId = app.globalData.wxlogin.userId
		
		var data = {
			pageNum: _that.data.mendianPage,
			pageSize: _that.data.mendianPageSize,
			lat: app.globalData.userLOC.latitude,
			lng: app.globalData.userLOC.longitude,
			userId: userId,
		}
		restDeptGet(data).then(res => {
			if(res.code == 0){
				console.log(res);
				var flagpage = Math.ceil(res.data.pages.total/_that.data.mendianPageSize);
				_that.setData({
					mendianFlag: _that.data.mendianPage>=flagpage ? false : true,
					mendianPage: _that.data.mendianPage+1,
					mendianList: _that.data.mendianList.concat(res.data.pages.rows),
					showPrice: res.data.showPrice
				});
				if(res.data.show_login=="0"){
					wx.showTabBar();
					_that.setData({showauthflag: false});
				}
			}else{
				_that.setData({
					mendianFlag: true,
					mendianPage: 1,
					mendianList: []
				});
			}
		});
	},
	restInformationBanner(){
		var _that = this;
		restInformationBanner(27).then(res => {
			console.log(res.data);
			if(res.code == 0){
				_that.setData({imgUrls: res.data});
			}
		});
	},
	
	/**
	 * 页面相关事件处理函数--监听用户下拉动作
	 */
	onPullDownRefresh: function () {
		var _that = this;
		wx.showNavigationBarLoading(); //在标题栏中显示加载图标
		_that.setData({
			mendianFlag: true,
			mendianPage: 1,
			mendianList: []
		});
		_that.restInformationBanner();
		_that.getDept();
		setTimeout(function(){
			wx.hideNavigationBarLoading(); //完成停止加载图标
		}, 1111);
		
	},
	
	/**
	 * 页面上拉触底事件的处理函数
	 */
	onReachBottom: function () {
		this.getDept();
	},
  
	getUser: function () {
		var _that = this;
		
		if(!_that.data.showauthflag){
			return false;
		}
		
		
		
		app.globalData.userInfo = http.getUserInfo();
		console.log(app.globalData);
		if(!app.globalData.userInfo || app.globalData.userInfo.length<=0){
			wx.getUserProfile({
			  desc: '用于完善会员资料', // 声明获取用户个人信息后的用途，后续会展示在弹窗中，请谨慎填写
			  success: (res) => {
					console.log(res)
					var userInfo = res.userInfo;
					var updateUserInfo = {
							headPic: userInfo.avatarUrl,
							nickname: userInfo.nickName,
							userId: app.globalData.wxlogin.userId
					};
					restUserCenter(updateUserInfo).then(res => {
						if(res.code == 0){
							restUserCenterByUserId().then(res => {
								console.log(res)
								if(res.code == 0){
									http.setUserInfo(res.data);
									app.globalData.userInfo = res.data;
									_that.setData({userInfo: res.data});
									
									wx.showTabBar();
									_that.setData({showauthflag: false});
								}
							});
						}else{
							wx.showToast({
								title:'哎呀，好像出问题了~',
								icon: 'none'
							})
						}
					});
			  }
			})
		}
	},
 
})
